package sr.myserver.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sr.myserver.vo.SelectData;
import sr.myserver.vo.common.Basic;
import sr.myserver.vo.common.PageRequest;
import sr.myserver.vo.common.PageResponse;
import sr.myserver.entity.SysParams;
import sr.myserver.mapper.SysParamsMapper;
import sr.myserver.service.SysParamsService;
import sr.myserver.commons.Result;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 参数信息
 *
 * @author sirui
 **/
@Service
public class SysParamsServiceImpl implements SysParamsService {

    @Autowired
    SysParamsMapper sysParamsMapper;

    /**
     * 查询详情
     *
     * @param id 主键
     * @return 实体对象
     */
    @Override
    public SysParams get(Serializable id) {
        return sysParamsMapper.selectById(id);
    }

    /**
     * 增加
     *
     * @param entity 请求参数
     * @return 响应码和响应消息
     */
    @Override
    public Basic.Response add(SysParams entity) {
        sysParamsMapper.insert(entity);
        return Result.ok();
    }

    /**
     * 修改
     *
     * @param entity 请求参数
     * @return 响应码和响应消息
     */
    @Override
    public Basic.Response update(SysParams entity) {
        sysParamsMapper.updateById(entity);
        return Result.ok();
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return 响应码和响应消息
     */
    @Override
    public Basic.Response deleteById(Serializable id) {
        sysParamsMapper.deleteById(id);
        return Result.ok();
    }

    /**
     * 列表查询
     *
     * @param request 请求参数
     * @return 列表数据集合
     */
    @Override
    public PageResponse<SysParams> queryTables(PageRequest request) {
        IPage<SysParams> data = sysParamsMapper.queryTables(request.getPage(), request.getQueryData());
        return new PageResponse(data.getCurrent(), data.getSize(), data.getTotal(), data.getRecords());
    }

    /**
     * 下拉查询
     *
     * @param request 请求参数
     * @return 列表数据集合
     */
    @Override
    public PageResponse<SelectData> querySelect(PageRequest request) {
        IPage<SelectData> data = sysParamsMapper.querySelect(request.getPage(), request.getQueryData());
        return new PageResponse(data.getCurrent(), data.getSize(), data.getTotal(), data.getRecords());
    }

    /**
     * 获取所有系统参数
     *
     * @return 每个分组下所有参数信息
     */
    @Override
    public Map<String, List<SelectData>> getAll() {
        List<String> list = sysParamsMapper.queryGroupId();
        Map<String, List<SelectData>> result = new HashMap<>(list.size());
        for (String gId : list) {
            result.put(gId, sysParamsMapper.queryByGroupId(gId));
        }
        return result;
    }

}

